/*
 * Class name: 			SItem
 * Class description: 	The SItem class is the class interface between
 * 						the back-end and the front end, it is a representative
 * 						class of ShoppingItem without the conflicting back-end
 * 						fields and libraries
 * Project:				GrocerWE
 */
package com.cse210.grocerwe.client;

import java.io.Serializable;

public class SItem implements Serializable{
	
	public static int ST_NORMAL = 0;
	public static int ST_CURR_SHOPPING = 1;
	public static int ST_BOUGHT = 2;
	
	//used for serialization
	private static final long serialVersionUID = 1L;
	
	//key as a string
	private String key;
	
	//name of item
	private String name;
    
	//type/category of item
	private String type;
	
	//the brand of the item
	private String brand;
	
	//the quantity of the item
	private int quantity;
	
	//the quantity size/type
	private String quantity_size;
	
	//the description of the item
	private String description;
	
    //Foreign Key, Person who added the item to the list
    private String owner;
    
    //Foreign Key, Person who is shopping/has bought, the item
    private String buyer;
    
    //group/list owning this item
    private String group;
    
    //boolean decided whether this item is part of the global inventory
    //or someones specific list item
	private boolean global_list;
    
    //status of item
    // 0 = normal, not bought, not in the process of shopping
    // 1 = Shopping, this item is currently being shopped for
    // 2 = Bought, this item has been bought by "buyer"
    private int status;
	
	/*
	 * Constructors
	 */
    
    public SItem()
    {
		this.key = null;
		this.name = null;
		this.type = null;
		this.brand = null;
		this.quantity = 0;
		this.quantity_size = null;
		this.description = null;
		this.owner = null;
		this.buyer = null;
		this.group = null;
		this.global_list = true;
		this.status = ST_NORMAL;
    }
    public SItem (String name, String type)
	{
		this.name = name;
		this.type = type;
		this.brand = null;
		this.quantity = 0;
		this.quantity_size = null;
		this.description = null;
		this.owner = null;
		this.buyer = null;
		this.group = null;
		this.global_list = true;
		this.status = ST_NORMAL;
	}
	public SItem (String name, String type, String owner, String group)
	{
		this.name = name;
		this.type = type;
		this.brand = null;
		this.quantity = 0;
		this.quantity_size = null;
		this.description = null;
		this.owner = owner;
		this.buyer = null;
		this.group = group;
		this.global_list = false;
		this.status = ST_NORMAL;
	}
	public SItem( String key, String name, String type, String brand, int quantity, String quantity_size,
				  String description, String owner, String buyer, String group, boolean isGlobal,int status)
	{
		this.key = key;
		this.name = name;
		this.type = type;
		this.brand = brand;
		this.quantity = quantity;
		this.quantity_size = quantity_size;
		this.description = description;
		this.owner = owner;
		this.buyer = buyer;
		this.group = group;
		this.global_list = isGlobal;
		this.status = status;
	}
	public SItem( String name, String type, String quantity_size, String description, String owner, String group)
	{
		this.name = name;
		this.type = type;
		this.brand = null;
		this.quantity = 0;
		this.quantity_size = quantity_size;
		this.description = description;
		this.owner = owner;
		this.buyer = null;
		this.group = group;
		this.global_list = false;
		this.status = ST_NORMAL;
	}

	/*
	 * Getters
	 */
	public String getKey()
	{
		return key;
	}
	public String getName()
	{
		return name;
	}
	public String getType()
	{
		return type;
	}
	public String getBrand()
	{
		return brand;
	}
	public int getQuantity()
	{
		return quantity;
	}
	public String getQuantitySize()
	{
		return quantity_size;
	}
	public String getDescription()
	{
		return description;
	}
	public String getOwner()
	{
		return owner;
	}
	public String getBuyer()
	{
		return buyer;
	}
	public String getGroup()
	{
		return group;
	}
	public boolean isGlobal()
	{
		return global_list;
	}
	public int getStatus()
	{
		return status;
	}
	
	/*
	 * Setters
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	public void setType(String type)
	{
		this.type = type;
	}
	public void setBrand(String brand)
	{
		this.brand = brand;
	}
	public void setQuantity(int quantity)
	{
		this.quantity = quantity;
	}
	public void setQuantitySize(String quantity_size)
	{
		this.quantity_size = quantity_size;
	}
	public void setDescription(String description)
	{
		this.description = description;
	}
	public void setOwner(String owner)
	{
		this.owner = owner;
	}
	public void setBuyer(String buyer)
	{
		this.buyer = buyer;
	}
	public void setGroup(String group)
	{
		this.group = group;
	}
	public void setStatus(int status)
	{
		this.status = status;
	}
	
	public SItem toSItem()
	{
		return new SItem(name,type);
	}
	
	public boolean isLocked()
	{
		return status != ST_NORMAL;
	}
	
	public synchronized boolean lock(String user)
	{
		if(isLocked())
			return false;
		else
		{
			status = ST_CURR_SHOPPING;
			this.buyer = user;
			return true;
		}
	}
	
	public boolean unlock(String user)
	{
		if(buyer.equals(user))
		{
			this.buyer = null;
			this.status = ST_NORMAL;
			return true;
		}
		else
			return false;
	}
	
	public boolean bought(String user)
	{
		if(buyer.equals(user))
		{
			this.status = ST_BOUGHT;
			return true;
		}
		else
			return false;
	}
	
	public String toString()
	{
		String s = "";
		
		s += "Item Name: " + this.getName() + "\n";
		s += "Item Quantatity: " + this.getQuantitySize() + "\n";
		s += "Item List: " + this.getGroup() + "\n";
		s += "Item Owner: " + this.getOwner() + "\n";
		s += "Item Status: " + this.getStatus() + "\n";
		if(this.getBuyer() == null)
			s += "Item Buyer: " + this.getBuyer() + "\n";
		
		return s;
	}
}